import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';

abstract class BtHiveStore<T> {
  Future<void> initHive() async {
    final appDocDir = await getApplicationDocumentsDirectory();
    Hive.init(appDocDir.path);
  }

  String boxName();

  String boxKey();

  // 打开Box
  Future<Box<T>> _openBox() async {
    return Hive.openBox<T>(boxName());
  }

  // 存储数据
  Future<void> putData(T data) async {
    final box = await _openBox();
    await box.put(boxKey(), data);
  }

  // 获取数据
  Future<T?> getData() async {
    final box = await _openBox();
    return box.get(boxKey());
  }

  // 删除数据
  Future<void> deleteData() async {
    final box = await _openBox();
    await box.delete(boxKey());
  }
}
